package cn.afactory.tygz.ui.base.views;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import cn.afactory.tygz.R;


/**
 * 包含有剩余可允许输入字数的EditText Created by pajk00214 on 2014/6/17.
 */
public class RestrictEditTextView extends LinearLayout {

    private int mMaxLength;
    private TextView mTextView;
    private PasteEditText mEditText;
    private ScrollView parent_scrollview;

    public ScrollView getParent_scrollview() {
        return parent_scrollview;
    }

    public RestrictEditTextView(Context context) {
        super(context);
    }

    public void setParent_scrollview(ScrollView parent_scrollview) {
        this.parent_scrollview = parent_scrollview;
    }

    public RestrictEditTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        addViews(context);
    }

    private void addViews(Context context) {
        View view = LayoutInflater.from(context).inflate(
                R.layout.layout_edit_with_text, this, true);
        mEditText = (PasteEditText) view.findViewById(R.id.et_input);
        mTextView = (TextView) view.findViewById(R.id.tv_count);
        mTextView.setTextColor(getResources().getColor(R.color.hint_color));
        mTextView.setText("0/" + mMaxLength);
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i,
                    int i2, int i3) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2,
                    int i3) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                String value = editable.toString();
                if (!TextUtils.isEmpty(value)) {
                    int length = value.length();
                    if (length > 0) {
                        mTextView.setTextColor(Color.BLACK);
                        mTextView.setText(length + "/" + mMaxLength);
                    } else {
                        mTextView.setTextColor(getResources().getColor(R.color.hint_color));
                        mTextView.setText("0/" + mMaxLength);
                    }
                } else {
                    mTextView.setTextColor(getResources().getColor(R.color.hint_color));
                    mTextView.setText("0/" + mMaxLength);
                }

            }
        });

    }

    public void setMaxLength(int maxLength) {
        this.mMaxLength = maxLength;
        mTextView.setText("0/" + mMaxLength);
        mEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(
                maxLength) });
    }

    public String getEditValue() {
        return mEditText == null ? "" : mEditText.getText().toString().trim();
    }

    public void setHint(String hintstr) {
        if (null != mEditText) {
            mEditText.setHint(hintstr);
            mEditText.setHintTextColor(getResources().getColor(
                    R.color.hint_color));
        }
    }

    public void setMinLines(int minLines) {
        if (null != mEditText) {
            mEditText.setMinLines(minLines);
        }
    }

    public void setMaxLines(int maxLines) {
        if (null != mEditText) {
            mEditText.setMaxLines(maxLines);
        }
    }

    public String getText() {
        return mEditText.getText().toString().trim();
    }
    public void setText(String desp) {
         if (!TextUtils.isEmpty(desp)){
             mEditText.setText(desp);
         }
    }

    /**
     * 清空文本框内容
     */
    public void clearText(){
        mEditText.setText("");
    }

    /**
     * 给输入框焦点，并强制弹出键盘
     */
    public void setFocused(){
        mEditText.requestFocus();
        ((InputMethodManager) mEditText.getContext()
                .getSystemService(Activity.INPUT_METHOD_SERVICE))
                .showSoftInput(mEditText,
                        InputMethodManager.SHOW_IMPLICIT);
    }

    int currentY;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (parent_scrollview == null) {
            return super.onInterceptTouchEvent(ev);
        } else {
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                // 将父scrollview的滚动事件拦截
                currentY = (int) ev.getY();
                setParentScrollAble(false);
                return super.onInterceptTouchEvent(ev);
            } else if (ev.getAction() == MotionEvent.ACTION_UP) {
                // 把滚动事件恢复给父Scrollview
                setParentScrollAble(true);
            } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            }
        }
        return super.onInterceptTouchEvent(ev);
    }

    /**
     * 是否把滚动事件交给父scrollview
     * 
     * @param flag
     */
    private void setParentScrollAble(boolean flag) {
        parent_scrollview.requestDisallowInterceptTouchEvent(!flag);
    }

    public void setMaxPasteLength(int length){
        mEditText.setMaxPasteTextLength(length);
    }

    public void showCount(Boolean t){
        if(t){
            mTextView.setVisibility(View.VISIBLE);
        } else {
            mTextView.setVisibility(View.GONE);
        }
    }

    /**
     * 设置横屏
     */
    public void setMatchParent(){
        ViewGroup.LayoutParams lp = mEditText.getLayoutParams();
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        mEditText.setLayoutParams(lp);
    }

}
